/*
 * Copyright (C) 2013 Fabiano Tarlao
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.easybenchmark.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import java.util.HashMap;
import org.easybenchmark.client.arbiter.ResultManager;
import org.easybenchmark.client.compatibilityutils.GWTBrowserUtils;

/**
 * Main entry point.
 *
 * @author fab
 */
public class MainEntryPoint implements EntryPoint {
    
    /** 
     * Creates a new instance of MainEntryPoint
     */
    public MainEntryPoint() {
        MainEntryPoint.applicationRoot = this;
    }
    
    public static MainEntryPoint applicationRoot;

    /** 
     * The entry point method, called automatically by loading a module
     * that declares an implementing class as an entry-point
     */
    
    protected static HashMap<String, Long> results = new HashMap<String, Long>();
    
    public static final Label logLabel = new Label("Log label");
    
    @Override
    public void onModuleLoad() {
        //they are locally created but the objetcs are added to rootpanel, they'll persist
        final Label label = new Label("Press Button");
        final Button button = new Button("Start Benchmark");
        final TextArea textArea = new TextArea();
        textArea.setWidth("80em");
        textArea.setHeight("20em");
        final VerticalPanel verticalPanel= new VerticalPanel();
        textArea.setReadOnly(true);
        
        
        verticalPanel.add(label);
        verticalPanel.add(button);
        verticalPanel.add(textArea);
        verticalPanel.add(logLabel);
        RootPanel.get().add(verticalPanel);
        
        button.addClickHandler(new ClickHandler() {
            
            @Override
            public void onClick(ClickEvent event) {
                label.setText("Benchmark is running");

                BenchmarkPool benchMarkPool = new BenchmarkPool(GWTBrowserUtils.getRealUserAgent(Window.Navigator.getUserAgent()));
                ResultManager resultManager  = benchMarkPool.executeAllBenchmarks();              
                textArea.setText(resultManager.getCSVColumnNames()+"\n"+resultManager.toCSVString()+
                        "\n"+ benchMarkPool.getStatusMessages());
                label.setText("Benchmark has ended");
            }
        }
        );   
    }
    
}
